#!/bin/bash

function test_ske()
{
	ske_algs=(AES_128_ECB
			AES_128_CBC
			AES_128_CFB
			AES_128_OFB
			AES_128_CTR 
			AES_192_ECB
			AES_192_CBC
			AES_192_CFB
			AES_192_OFB
			AES_192_CTR
			AES_256_ECB
			AES_256_CBC
			AES_256_CFB
			AES_256_OFB
			AES_256_CTR
			)

	local alg=
	for alg in ${ske_algs[@]};do
		./abcrypto -a $alg -s 16 -e 20000
	done

	sm4_algs=(SM4_ECB
	SM4_CBC
	SM4_CTR
	SM4_CFB
	SM4_OFB
	)
	for alg in ${sm4_algs[@]};do
		./abcrypto -a $alg -s 16 -e 20000 -B nettle
	done
}


function test_hash()
{
	hash_algs=(
		HASH_MD5		
		HASH_SHA1
		HASH_SHA224
		HASH_SHA256
		HASH_SHA384
		HASH_SHA512
		HASH_SHA3_224
		HASH_SHA3_256
		HASH_SHA3_384
		HASH_SHA3_512
	)
	
	local alg=
	for alg in ${hash_algs[@]};do
		./abcrypto -a $alg -s 1 -e 20000 
	done

	./abcrypto -a HASH_SM3 -B nettle -s 1 -e 20000 
}

function test_hmac()
{
	hmac_algs=(
		HMAC_MD5
		HMAC_SHA1
		HMAC_SHA224
		HMAC_SHA256
		HMAC_SHA384
		HMAC_SHA512
		HMAC_SHA3_224
		HMAC_SHA3_256
		HMAC_SHA3_384
		HMAC_SHA3_512
	)

	local alg=
	for alg in ${hmac_algs[@]};do
		./abcrypto -a $alg -s 1 -e 20000 
	done

	./abcrypto -a HMAC_SM3 -B nettle -s 1 -e 20000 
}


run=all
function main()
{
	if [ $# -gt 0 ]; then
		ARGS=`getopt -l cipher,hash,hmac,help -- "$@"`
		if [ $? != 0 ];then
				echo "Terminating..."
				exit 1
		fi
		
		#重新排列参数顺序
		# eval  "${ARGS}"
		#eval "${ARGS}"
		#通过shift和while循环处理参数
		while :
		do
			case $1 in
				--help)
					usage
					;;
				--cipher)
					run=cipher
					# break
					;;
				--hash)
					run=hash
					# break
					;;
				--hmac)
					run=hmac
					;;
				--)
					shift
					break
					;;
				*)
					break
					;;
			esac
		shift
		done
	fi

	# 记录启动时间
	start_time=$(date +%s%N)
	echo "Script start time: $(date -d @$(($start_time / 1000000000)) +'%Y-%m-%d %H:%M:%S.%N')"

    echo "Run $run cases:"

	case "$run" in
    "cipher")
		test_ske
        ;;
    "hash")
		test_hash
        ;;
    "hmac")
		test_hmac
        ;;
    "all")
		test_ske
		test_hash
		test_hmac
        ;;
	esac

	# 记录结束时间
	end_time=$(date +%s%N)
	echo "Script end time: $(date -d @$(($end_time / 1000000000)) +'%Y-%m-%d %H:%M:%S.%N')"

	time_diff=$(echo "scale=6; ($end_time - $start_time) / 1000000000" | bc)
	echo "Script running time: $time_diff seconds" 
}


main $@



